#
# Cookbook Name:: om
# Recipe:: setupdb2
#
# Copyright 2013, IBM Corporation
#
# All rights reserved - Do Not Redistribute
#

class Chef::Recipe
include Orchestrate
end


deployment = node[:deployment]
zkep = "#{node[:zookeeper][:ip]}:#{node[:zookeeper][:port]}"
chef_gem 'zk'

include_recipe "gitclientsetup::default"
include_recipe "db2::install"

startinstr "Setup DB2 tablespaces and schemas"


dbcreated=`(. ~db2inst1/sqllib/db2profile; db2 list database directory | grep alias |grep #{node[:om][:omdbname]})`
if $? != 0
	bash "Database configuration" do
		user "db2inst1"
		code <<-EOH
			#{node[:db2][:installDir]}/bin/db2 -x 'CREATE DATABASE #{node[:om][:omdbname]}'
			#{node[:db2][:installDir]}/bin/db2 -x 'connect to #{node[:om][:omdbname]} user db2inst1 using #{node[:db2][:password]}'
			#{node[:db2][:installDir]}/bin/db2 -x 'CREATE BUFFERPOOL OMS32K_BP IMMEDIATE SIZE 100 PAGESIZE 32k'
			#{node[:db2][:installDir]}/bin/db2 -x 'CREATE BUFFERPOOL OMS_TMP_32K_BP IMMEDIATE SIZE 100 PAGESIZE 32k'
			#{node[:db2][:installDir]}/bin/db2 -x 'CREATE TABLESPACE OMS_32K_TS PAGESIZE 32k MANAGED BY AUTOMATIC STORAGE BUFFERPOOL OMS32K_BP'
			#{node[:db2][:installDir]}/bin/db2 -x 'CREATE TEMPORARY TABLESPACE OMS_TMP_32K_TS PAGESIZE 32k MANAGED BY AUTOMATIC STORAGE BUFFERPOOL OMS_TMP_32K_BP'
			#{node[:db2][:installDir]}/bin/db2 -x 'GRANT USE OF TABLESPACE OMS_32K_TS to public' 
			#{node[:db2][:installDir]}/bin/db2 -x 'CREATE SCHEMA CTMETA AUTHORIZATION db2inst1'
			#{node[:db2][:installDir]}/bin/db2 -x 'CREATE SCHEMA CTSTAT AUTHORIZATION db2inst1'
			#{node[:db2][:installDir]}/bin/db2 -x 'CREATE SCHEMA CTCONFIG AUTHORIZATION db2inst1'
			#{node[:db2][:installDir]}/bin/db2 -x 'CREATE SCHEMA CTTRAN AUTHORIZATION db2inst1'
			#{node[:db2][:installDir]}/bin/db2 -x 'CREATE SCHEMA CTDFBI AUTHORIZATION db2inst1'
			#{node[:db2][:installDir]}/bin/db2 -x 'CREATE SCHEMA CTSTGBI AUTHORIZATION db2inst1'
			#{node[:db2][:installDir]}/bin/db2 -x 'disconnect ALL'
		EOH
	end
end

bash "Set DB2 Registry Variables" do
	user "db2inst1"
	code <<-EOH

		~db2inst1/sqllib/adm/db2set DB2_CAPTURE_LOCKTIMEOUT=OFF
		~db2inst1/sqllib/adm/db2set DB2_SKIPINSERTED=ON
		~db2inst1/sqllib/adm/db2set DB2_USE_ALTERNATE_PAGE_CLEANING=ON
		~db2inst1/sqllib/adm/db2set DB2_NUM_CKPW_DAEMONS=0
		~db2inst1/sqllib/adm/db2set DB2_EVALUNCOMMITTED=ON
		~db2inst1/sqllib/adm/db2set DB2_SELECTIVITY=ON
		~db2inst1/sqllib/adm/db2set DB2_SKIPDELETED=ON
		~db2inst1/sqllib/adm/db2set DB2_MMAP_WRITE=OFF
		~db2inst1/sqllib/adm/db2set DB2_MMAP_READ=OFF
		~db2inst1/sqllib/adm/db2set DB2LOCK_TO_RB=STATEMENT
		~db2inst1/sqllib/adm/db2set DB2COMM=tcpip
		~db2inst1/sqllib/adm/db2set DB2_PARALLEL_IO=ON
		~db2inst1/sqllib/adm/db2set DB2_PINNED_BP=
		~db2inst1/sqllib/adm/db2set DB2MEMMAXFREE=
		~db2inst1/sqllib/adm/db2set DB2_ENABLE_BUFPD=
		~db2inst1/sqllib/adm/db2set DB2_NUM_CKPW_DAEMONS=0
		~db2inst1/sqllib/adm/db2set DB2_COMPATIBILITY_VECTOR=ORA
	EOH
end

bash "Set Database Configuration Settings" do
	user "db2inst1"
	code <<-EOH
			#{node[:db2][:installDir]}/bin/db2 update db cfg for #{node[:om][:omdbname]} using SELF_TUNING_MEM ON
			#{node[:db2][:installDir]}/bin/db2 update db cfg for #{node[:om][:omdbname]} using LOCKLIST AUTOMATIC
			#{node[:db2][:installDir]}/bin/db2 update db cfg for #{node[:om][:omdbname]} using MAXLOCKS AUTOMATIC
			#{node[:db2][:installDir]}/bin/db2 update db cfg for #{node[:om][:omdbname]} using PCKCACHESZ AUTOMATIC
			#{node[:db2][:installDir]}/bin/db2 update db cfg for #{node[:om][:omdbname]} using SHEAPTHRES_SHR AUTOMATIC
			#{node[:db2][:installDir]}/bin/db2 update db cfg for #{node[:om][:omdbname]} using SORTHEAP AUTOMATIC
			#{node[:db2][:installDir]}/bin/db2 update db cfg for #{node[:om][:omdbname]} using NUM_IOCLEANERS AUTOMATIC
			#{node[:db2][:installDir]}/bin/db2 update db cfg for #{node[:om][:omdbname]} using NUM_IOSERVERS AUTOMATIC
			#{node[:db2][:installDir]}/bin/db2 update db cfg for #{node[:om][:omdbname]} using DFT_PREFETCH_SZ AUTOMATIC
			#{node[:db2][:installDir]}/bin/db2 update db cfg for #{node[:om][:omdbname]} using MAXAPPLS AUTOMATIC
			#{node[:db2][:installDir]}/bin/db2 update db cfg for #{node[:om][:omdbname]} using AVG_APPLS AUTOMATIC
	EOH
end

bash "Open Database Port" do
    user "root"
    code <<-EOH
        /sbin/iptables -A INPUT -p tcp --dport 50000 -j ACCEPT
		service iptables save
    EOH
end

#TODO:  Should set SKIP_INSERTED, SKIP_DELETED, etc...

ruby_block "Publishing Runtime Database Status to ZooKeeper." do
    block do
        i_url = "/deployments/" + deployment + "/config/om/runtime/db2/status"
        dep = Dependency.new(i_url, node[:client][:status], zkep)
        dep.publish!
    end
end

#install newrelic agent for db2 after db2 installation
include_recipe "newrelic::install_db2"

stopinstr "Setup DB2 tablespaces and schemas"
